package org.wanghong.gift.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wanghong.gift.client.SmsValidateCodeClient;
import org.wanghong.gift.domain.User;
import org.wanghong.gift.exception.asserts.Assert;
import org.wanghong.gift.exception.statuCode.StatusCode;
import org.wanghong.gift.mapper.UserMapper;
import org.wanghong.gift.service.ISmsValidateCodeService;
import org.wanghong.gift.util.AjaxResult;

import java.util.List;

@Service
public class SmsValidateCodeServiceImpl implements ISmsValidateCodeService {
    @Autowired
    private UserMapper userMapper;
    //短信验证码依赖
    @Autowired
    private SmsValidateCodeClient smsValidateCodeClient;
    @Override
    public AjaxResult sendRegisterCode(String phone) {
        //空判断
        Assert.assertNotEmpty(phone, StatusCode.CODE_400);

        //判断用户是否已经存在
        List<User> users = userMapper.selectList(new EntityWrapper<User>().eq("phone", phone));
        Assert.assertListIsEmpty(users,StatusCode.CODE_400_USER_EXSIT);

        //远程调用发送短信验证码
        return smsValidateCodeClient.sendRegisterCode(phone);
    }

}
